package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Movement;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.mongo.CommentApi;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.Date;
import java.util.List;

@Service
public class CommentApiImpl implements CommentApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override  //这个方法是对圈子的点赞、喜欢、评论 对小视频的点赞、评论 对评论的点赞这些动态保存数据
    public Integer saveComment(Comment comment) { //针对6个操作保存
//        1、保存comment数据之前补齐comment缺少的数据

        comment.setId(new ObjectId());
        comment.setCreated(new Date().getTime());

        ObjectId publishId = comment.getPublishId(); //有可能是圈子的id 有可能是小视频的id 也有可能是评论的id
        if( comment.getCommentType()==1|| comment.getCommentType()==2|| comment.getCommentType()==3){
//            publishId就是圈子的id

//          1、在圈子的点赞数+1
//            movement.setLikeCount(movement.getLikeCount()+1);
//         模拟mysql执行mongo   update movement_detail set likeCount=likeCount+1 where id=?
            Query query = new Query(Criteria.where("id").is(publishId));
            Update update = new Update();
            update.inc(comment.getCol(),1); //有可能对点赞+1  有可能对喜欢数+1 也有可能对评论数+1
//            commentType==1 对点赞数+1 likeCount   commentType==2  对评论数+1 commentCount    commentType==3  对喜欢数+1 loveCount

            mongoTemplate.updateFirst(query,update,Movement.class);
//           2、 开始补齐comment的数据
            Movement movement = mongoTemplate.findById(publishId, Movement.class); //为了获取这个圈子的发布人id
            Long userId = movement.getUserId();
            comment.setPublishUserId(userId); //当前动态的发布人.
//           3、comment数据已经补齐了就可以保存了
            mongoTemplate.save(comment);

            // 4、返回这个圈子最后的点赞数
            return comment.getMovementCount(movement);


        }else if(comment.getCommentType()==4||comment.getCommentType()==5){
//            publishId就是小视频的id TODO

        } else if(comment.getCommentType()==6){
//            publishId就是评论的id TODO
//            1、把评论的点赞数+1
            //         模拟mysql执行mongo   update comment set likeCount=likeCount+1 where id=?
            Query query = new Query(Criteria.where("id").is(publishId));
            Update update = new Update();
            update.inc("likeCount",1); //有可能对点赞+1  有可能对喜欢数+1 也有可能对评论数+1
            mongoTemplate.updateFirst(query,update,Comment.class);
//            2、保存comment
            Comment comment1 = mongoTemplate.findById(publishId, Comment.class);
            comment.setPublishUserId(comment1.getUserId()); //点赞的这个评论是谁发布的
            mongoTemplate.save(comment);

//             3、返回这个评论最后的点赞数据
           return comment1.getLikeCount();

        }
        return null;
    }

    @Override
    public Integer deleteComment(Comment comment) {
        ObjectId publishId = comment.getPublishId(); //有可能是圈子的id 有可能是小视频的id 也有可能是评论的id
        if( comment.getCommentType()==1|| comment.getCommentType()==2|| comment.getCommentType()==3){
//            publishId就是圈子的id
//          1、在圈子的点赞数-1
//         模拟mysql执行mongo   update movement_detail set likeCount=likeCount+1 where id=?
            Query query = new Query(Criteria.where("id").is(publishId));
            Update update = new Update();
            update.inc(comment.getCol(),-1); //有可能对点赞-1  有可能对喜欢数-1 也有可能对评论数-1
            mongoTemplate.updateFirst(query,update,Movement.class);
//           2、 删除comment的数据  delete from  comment  where publishId=?? and userId=? and commentType=1
            Query query1 = new Query(Criteria.where("publishId").is(publishId).and("userId").is(comment.getUserId()).and("commentType").is(comment.getCommentType()));
            mongoTemplate.remove(query1,Comment.class);

//            3、查询最新的圈子数据，为了获取最后的点赞数
            Movement movement = mongoTemplate.findById(publishId, Movement.class); //为了获取这个圈子的发布人id
            return comment.getMovementCount(movement);

        }else if(comment.getCommentType()==4||comment.getCommentType()==5){
//            publishId就是小视频的id TODO

        } else if(comment.getCommentType()==6){
//            publishId就是评论的id TODO
//            1、把评论的点赞数-1
            //         模拟mysql执行mongo   update comment set likeCount=likeCount+1 where id=?
            Query query = new Query(Criteria.where("id").is(publishId));
            Update update = new Update();
            update.inc("likeCount",-1);
            mongoTemplate.updateFirst(query,update,Comment.class);
//            2、删除comment
//            delete from comment where publishId=?? and commentType=6
            Query query1 = new Query(Criteria.where("publishId").is(publishId).and("commentType").is(6));
            mongoTemplate.remove(query1,Comment.class);

//             3、返回这个评论最后的点赞数据

            return mongoTemplate.findById(publishId,Comment.class).getLikeCount();


        }
        return null;
    }


    @Override //  分页查询圈子的评论数据
    public PageResult queryComments(Integer page, Integer pagesize, String movementId) {

        Query query = new Query(Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(2));
        query.limit(pagesize).skip((page-1)*pagesize);
        query.with(Sort.by(Sort.Direction.DESC,"created"));

        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);

        return new PageResult(page,pagesize,(int)count,commentList);
    }

    @Override
    public PageResult queryCommentPeople(Integer page, Integer pagesize, int commentType, Long publishUserId) {
        Query query = new Query(Criteria.where("publishUserId").is(publishUserId).and("commentType").is(commentType));
        query.limit(pagesize).skip((page-1)*pagesize);
        query.with(Sort.by(Sort.Direction.DESC,"created"));

        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);

        return new PageResult(page,pagesize,(int)count,commentList);
    }
}
